﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Web.UI;
using UMC.Web;
using UMC.Wiki.Entities;

namespace UMC.Wiki.Activities
{
    class SubjectImageActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {

            var strId = this.AsyncDialog("Id", g =>
             {
                 return new Web.UITextDialog() { Title = "主题" };
             });

            var sid = UMC.Data.Utility.UInt64Parse(strId);
            var seq = UMC.Data.Utility.IntParse(this.AsyncDialog("Seq", "0"), 0);
            var webr = UMC.Web.WebResource.Instance();
            var Url = this.AsyncDialog("Url", gK =>
            {
                var args = request.Arguments;
                var ui = UISection.Create();

                var title = new UITitle("正文图片");

                ui.Title = title;

                var items = new List<WebMeta>();

                var su2bs = DataFactory.Instance().Content(sid.Value) ?? new SubjectContent { Id = sid };

                var celss = UMC.Data.JSON.Deserializes<WebMeta>((String.IsNullOrEmpty(su2bs.DataJSON) ? "[]" : su2bs.DataJSON)) ?? new UMC.Web.WebMeta[] { };
                foreach (var pom in celss)
                {
                    switch (pom["type"])
                    {
                        case "CMSImage":
                            var value = pom.GetMeta("value");
                            var src = value["src"];
                            if (String.IsNullOrEmpty(src) == false)
                            {
                                items.Add(new UMC.Web.WebMeta().Put("src", src).Put("click", new Web.UIClick(new UMC.Web.WebMeta(args).Put(gK, src))
                                {
                                    Model = request.Model,
                                    Command = request.Command
                                }));

                            }

                            break;
                    }
                }
                if (items.Count > 0)
                {
                    var nine = new UMC.Web.WebMeta().Put("images", items);
                    ui.NewSection().Add(UICell.Create("NineImage", nine));
                }
                else
                {
                    ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "图文未有图片").Put("icon", "\uF016"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),

                      new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));
                }
                ui.SendTo($"Subject.{request.Command}", this.Context);
            });
            if (seq == 0)
            {
                var indexs = DataFactory.Instance().Subject(sid.Value).ImageIndexs ?? 0;
                var ls = Utility.BitIndex(indexs);
                if (ls.Length == 0)
                {
                    indexs = seq = 1;
                }
                else
                {
                    seq = ls[ls.Length - 1] + 1;
                }


                DataFactory.Instance().Put(new Subject { Id = sid.Value, ImageIndexs = Utility.BitIndex(indexs, seq, true) });


            }

            response.Redirect("System", "Image", new WebMeta().Put("id", sid).Put("index", seq).Put("media_id", Url), true);


        }

    }
}